<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class LoginForm extends CFormModel
{
	public $username;
	public $password;
    public $auto;
	private $_identity;

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
    public function rules()
    {
        return array(
            array('username','email','message'=>'noemail'),
            array('username','length', 'min'=>2,'tooShort'=>'-20'),
            array('username','length', 'max'=>50,'tooLong'=>'-21'),
            array('username','required','message'=>'-22'),//用户名已存在
            array('password', 'required','message'=>'-4'),//密码必须填写
            array('auto', 'safe'),
        );
    }
    /**
     * Logs in the user using the given username and password in the model.
     * @return boolean whether login is successful
     */
    public function login()
    {
        $this->_identity = new UserIdentity($this->username,$this->password,$this->auto);
        $status = $this->_identity->authenticate();
        if($status == true){
            $duration = '';
            Yii::app()->user->login($this->_identity,$duration);
            return true;
        }else{
            return false;
        }
    }





}
